//
//  RawUserView.m
//  Final
//
//  Created by student on 2021/12/14.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RawUserView.h"

@interface RawUserView ()

@property(strong, nonatomic) RawUser* rawuser;
@property(strong, nonatomic) UIViewController* scv;

@property(strong, nonatomic) UIImageView* iv;
@property(strong, nonatomic) UILabel* label;
@property(strong, nonatomic) UIGestureRecognizer* tapGesture;

@end

@implementation RawUserView

-(RawUserView*)initWithRawUser: (RawUser*)ru
             andViewController: (UIViewController*)scv
{
    self = [super init];
    self.rawuser = ru;
    self.scv = scv;
    
    self.iv.image = self.rawuser.head_img;
    [self addSubview:self.iv];
    
    self.label.text = self.rawuser.name;
    [self addSubview:self.label];
    
    [self addGestureRecognizer:self.tapGesture];
    
    return self;
}

- (void) resetRawuser:(RawUser *)rawuser
    andViewController:(UIViewController*)scv
{
    self.rawuser = rawuser;
    self.iv.image = rawuser.head_img;
    self.label.text = rawuser.name;
    self.scv = scv;
}

//Lazy load
-(UIImageView*)iv
{
    if(_iv == nil){
        _iv = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 35, 35)];
        _iv.layer.cornerRadius = 5;
        _iv.contentMode = UIViewContentModeScaleAspectFit;
        _iv.clipsToBounds = YES;
    }
    return _iv;
}

-(UILabel*)label
{
    if(_label == nil){
        _label = [[UILabel alloc]initWithFrame:CGRectMake(45, 12, 100, 20)];
        [_label setFont:[UIFont systemFontOfSize:20]];
    }
    return _label;
}

-(UIGestureRecognizer*)tapGesture
{
    if(_tapGesture == nil){
        _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapUser)];
    }
    return _tapGesture;
}

-(void)tapUser
{
    NSLog(@"Tap the user!");
    User* user = [[ModelController getInstance] getUser:self.rawuser.u_id];
    UserDetailViewController* uvc = [[UserDetailViewController alloc]initWithUser:user canBeModified:NO];
    NSLog(@"%@", self.scv);
    [self.scv.navigationController pushViewController:uvc animated:YES];
}

@end
